package edu.bu.met683.TravelDroid.Tests;

import java.util.ArrayList;
import java.util.List;

import android.app.ListActivity;
import android.app.ProgressDialog;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.widget.ListView;
import android.widget.TextView;

public class TourList extends ListActivity
{
	public List<Tour> tours;
	private ProgressDialog progressDialog;
	private TextView noTours;
	private TourListAdapter tourListAdapter;
	
	
	private final Handler handler = new Handler() {  
	      @Override 	  
	        public void handleMessage(final Message msg) { 
	    	  try
	    	  {
	            TourList.this.progressDialog.dismiss(); 
	            if ((tours == null) || (tours.size() == 0)) 
	            { 
	                TourList.this.noTours.setText("No Tours Present"); //set the text for the text view in layout
	            } 
	            else 
	            { 
	            	TourList.this.tourListAdapter = new TourListAdapter(TourList.this, tours); 
	            	TourList.this.setListAdapter(tourListAdapter); 
	            } 
	    	  }
	    	  catch(Exception ee)
	    	  {
	    		  Log.e("ERROR", "ERROR IN CODE TourList handler:"+ee.toString());
	    	  }
	       } 
	    }; 
	

	@Override
	public void onCreate(Bundle savedInstanceState) {
	  super.onCreate(savedInstanceState);
	  try
	  {
		  progressDialog = new ProgressDialog(TourList.this);
		  tours = new ArrayList<Tour>();
		  this.setContentView(R.layout.tourviewlist);
	 
		  this.noTours = (TextView) findViewById(R.id.noTours);
	  
		  final ListView listView = this.getListView(); //gets the activities listview widget
		  listView.setItemsCanFocus(false);
		  listView.setChoiceMode(ListView.CHOICE_MODE_SINGLE);
		  listView.setEmptyView(noTours); //sets the view to show if the list is empty
	 
		  //PopulateNearByTours();	  
		  //setListAdapter(new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, TourArray));
		  getListView().setTextFilterEnabled(true);
	  }
	  catch(Exception ee)
	  {
		  Log.e("ERROR", "ERROR IN CODE TourList onCreate:"+ee.toString());
	  }
	}
	
    @Override 
    protected void onResume() { 
    	try
    	{
    		super.onResume(); 
        	this.PopulateNearByTours();
    	}
  	  	catch(Exception ee)
  	  	{
  	  		Log.e("ERROR", "ERROR IN CODE:"+ee.toString());
  	  	}
  	} 
    
	  
	static final String[] TourArray = new String[] { "Freedom Trail", "Salem", "Gloucester"};
	
	private int PopulateNearByTours()
	{
		try
		{			
			tours.clear();
			tours.add(new Tour(1,"Freedon Trail", "this tour expores historic downtown Boston", 10.0));
			tours.add(new Tour(2,"Salem Heritage", "Description of Salem trail", 5.0));
			tours.add(new Tour(3, "Gloucester Maritime", "Description of Gloucester trail", 7.0));
			
			TourList.this.handler.sendEmptyMessage(0);
			return tours.size();
		}
		catch(Exception ee)
		{
			Log.e("ERROR", "ERROR IN CODE:"+ee.toString());
			return 0;
		}
	}
	
}
